#!/bin/bash
# SPDX-License-Identifier: MulanPSL-2.0+
# Copyright (c) 2020 Huawei Technologies Co., Ltd. All rights reserved.

# this script is used to do base pack for lkp-tests.
# if you want to do the pack with a tag, use:
#   ./run {{ tag }}
# case the tag already exists, you may need to confirm to use the existing tag or not.
# case the tag is new, it will add the new tag and use it do to the pack.
# before use this tool, you need to set following keys to $home/.config/compass-ci/pack.yaml first:
#   - lkp_pack_server
#   - lkp_pack_tag_user
#   - lkp_rsync_server
# lkp_pack_server: the server on which you do the pack action to generate the base package.
# lkp_pack_tag_user: the user you do the pack action to generate the base package.
# lkp_rsync_server: the destination server that you rsync the base pack to.
# case you want to do the pack action without rsync it, just set lkp_rsync_server the same as lkp_pack_server.

DIR=$(dirname $(realpath $0))
. $(dirname $DIR)/defconfig.sh

TAG=$1

load_pack_vars

[[ $HOSTNAME == $lkp_pack_server ]] && [[ $USER == $lkp_pack_tag_user ]] || unset TAG

[[ -n $TAG ]] && {
	check_tag=$(git -C $LKP_SRC tag -l $TAG)
	if [[ -n $check_tag ]]
	then
		echo "Warning: Tag $TAG already exists."
		echo "It will rollback to it if you want to use it to do the package."
		echo -n "Continue(Y/y) or specify a new one(N/n): "
		read answer

		[[ $answer =~ ^[Nn] ]] && exit
		# case use an existing tag to do the pkg,
		# first roll back to the tag, and then do the pkg.
		git -C $LKP_SRC reset --hard $TAG
	else
		git -C $LKP_SRC tag $TAG
	fi
}

[[ $ARCH 	]] || ARCH=$(uname -m)
[[ $LKP_SRC	]] || LKP_SRC=/c/lkp-tests

cmd=(
	docker run
	--rm
	-e ARCH=$ARCH
	-e LKP_SRC=$LKP_SRC
	-v $LKP_SRC:$LKP_SRC
	-v $DIR/bin:/root/bin
	-v /srv/initrd/lkp/${lkp_initrd_user:-latest}:/osimage/user/lkp
	alpine:lkp
	/root/bin/pack-lkp.sh
)

"${cmd[@]}"

echo "result: /srv/initrd/lkp/${lkp_initrd_user:-latest}/lkp-${ARCH}.cgz"

# directory to store the base cgz is: /srv/upload-file/$repo_name/${ARCH}
[[ -n $TAG ]] && {
	repo_name=$(echo $LKP_SRC | awk -F'/' '{print $NF}')

	[[ -d "/srv/upload-files/$repo_name/${ARCH}" ]] || mkdir -p "/srv/upload-files/$repo_name/${ARCH}"
	cp "/srv/initrd/lkp/${lkp_initrd_user:-latest}/lkp-${ARCH}.cgz" "/srv/upload-files/$repo_name/${ARCH}/$TAG.cgz"
	echo "result: /srv/upload-files/$repo_name/${ARCH}/$TAG.cgz"

	# when doing the package with new tag, sync the base cgz to ${lkp_rsync_server} server.
	# case $lkp_pack_server == $lkp_rsync_server, ignore the rsync
	[[ $lkp_pack_server == $lkp_rsync_server ]] || {
		rsync -aPz "/srv/upload-files/$repo_name/${ARCH}/$TAG.cgz" "${lkp_pack_tag_user}@${lkp_rsync_server}:/srv/upload-files/$repo_name/${ARCH}"
		echo "$lkp_rsync_server result: :/srv/upload-files/$repo_name/${ARCH}/$TAG.cgz"
	}
}

# rsync lkp-${ARCH}.cgz if needed
if [ -x /usr/local/bin/rsync-lkp-cgz ]; then
	/usr/local/bin/rsync-lkp-cgz "${lkp_initrd_user:-latest}" "${ARCH}"
fi

git -C $LKP_SRC pull > /dev/null
